package com.appspot.didit.client.workitem;

import java.util.ArrayList;
import com.appspot.didit.client.tag.*;
import com.appspot.didit.client.ui.EnhancedTextBox;
import com.appspot.didit.client.util.Util;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.*;

public class WorkitemListView extends Composite implements WorkitemListPresenter.View {
	
	public FlexTable table;
	Label l_description, l_duration, l_tags, l_time;
    public WorkitemListView() {

    	table = new FlexTable();
    	l_description = new Label("   Was wurde gemacht  ");
		l_description.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		l_duration= new Label("     Dauer     ");
		l_duration.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		l_tags= new Label("        Projekte              ");
		l_tags.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		l_time= new Label("   Datum   ");
		l_time.getElement().getStyle().setProperty("backgroundColor", "#ADDFFF");
		table.setWidget(0, 0, l_tags);
		table.setWidget(0, 1, l_description);
		table.setWidget(0, 2, l_duration);
		table.setWidget(0, 3, l_time);
        // Wrap the content in a DecoratorPanel
        DecoratorPanel decPanel = new DecoratorPanel();
        decPanel.setWidget(table);
        decPanel.setWidth("400px");
        initWidget(decPanel);
    }

    public Widget getWidget() {
            return this;
    }

    public void reset() {
    	table.clear();
    	table.setWidget(0, 0, l_tags);
		table.setWidget(0, 1, l_description);
		table.setWidget(0, 2, l_duration);
		table.setWidget(0, 3, l_time);
    }
    
    public void update(ArrayList<Workitem> workitemList, ChangesListener cl) {
    	table.clear();
    	table.setWidget(0, 0, l_tags);
		table.setWidget(0, 1, l_description);
		table.setWidget(0, 2, l_duration);
		table.setWidget(0, 3, l_time);
    	if(workitemList != null) {
    		int rows = (workitemList.size()>10? 10 : workitemList.size());
			for(int i = 0, rowCount = 1; i < rows; i++, rowCount++) {
				Workitem wi = workitemList.get(i);
				EnhancedTextBox description = new EnhancedTextBox(wi.getDescription(), rowCount);
				description.setStylePrimaryName("tb-noborder");
				description.setListener(cl);
				
				EnhancedTextBox duration = new EnhancedTextBox(wi.getDuration() + "", rowCount);
				duration.setStylePrimaryName("tb-noborder");
				duration.setListener(cl);
				
				final EnhancedTextBox hour = new EnhancedTextBox((int)wi.getDuration() + "", rowCount);
				hour.setStylePrimaryName("tb-noborder");
				hour.setListener(cl);
				hour.setMaxLength(3);
			    hour.setWidth("30");
			    hour.setTextAlignment(TextBoxBase.ALIGN_RIGHT);
			    hour.addValueChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						if(event.getValue().equals("")) {
							hour.setValue("0");
							return;
						}
						try {
							Integer.parseInt(event.getValue());
						} catch(Exception e) {
							hour.setValue("0");
						}
					}
			    	
			    });
			    
			    int minutes = Util.getMinutesFromDouble(wi.getDuration());
				final EnhancedTextBox minute = new EnhancedTextBox( (minutes<10?"0":"") + minutes, rowCount);
				minute.setStylePrimaryName("tb-noborder");
				minute.setListener(cl);
				minute.setMaxLength(3);
			    minute.setWidth("30");
			    minute.setTextAlignment(TextBoxBase.ALIGN_LEFT);
			    minute.addValueChangeHandler(new ValueChangeHandler<String>() {

					@Override
					public void onValueChange(ValueChangeEvent<String> event) {
						if(event.getValue().equals("")) {
							minute.setValue("00");
							return;
						}
						try {
							if(Integer.parseInt(event.getValue()) < 10) {
								minute.setValue("0" + Integer.parseInt(event.getValue()));
							}
						} catch(Exception e) {
							minute.setValue("00");
						}
					}
			    	
			    });
				
				final EnhancedTextBox tags = new EnhancedTextBox(wi.getTagString(), rowCount);
				tags.addFocusHandler(new FocusHandler() {
					public void onFocus(FocusEvent event) {
						TagCloud tc = TagManager.getInstance().getTagCloud();
						if(tc != null && !tc.isShowing()) {
							tc.show(tags);
						}
						
					}
				});
				tags.addKeyDownHandler(new KeyDownHandler() {
					@Override
					public void onKeyDown(KeyDownEvent event) {
						if (event.getNativeKeyCode() == KeyCodes.KEY_TAB) {
							 TagCloud tc = TagManager.getInstance().getTagCloud();
								if(tc != null && tc.getPartner().equals(tags)) {
									tc.hide();
								}
						 }
					}
		        	
		        });
				tags.setStylePrimaryName("tb-noborder");
				tags.setListener(cl);
				
				FlexTable dauer = new FlexTable();
				dauer.setWidget(0, 0, hour);
			    dauer.setWidget(0, 1, new HTML(":"));
			    dauer.setWidget(0, 2, minute);
			    
				table.setWidget(rowCount, 0, tags);
				table.setWidget(rowCount, 1, description);
				table.setWidget(rowCount, 2, dauer);
				table.setWidget(rowCount, 3, new Label(Util.getDate((wi.getDate()))));
			}
		}
    }
	
    public HasWidgets getTable() {
    	return table;
    }

}
